/**
 * @author Alfort Robert - Köckinger Martin
 * This Class handles the Encode Process
 * @version 2.1
 */

package at.fhj.itm;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

/** Call when Activity Decode is active */
public class QRCodeEncode extends Activity {
	
	public Button encodeContact; /** Creating a Button for encoding from Contact */
	public Button encodeText; /** Creating a Button for encoding from Text */
	public EditText inputTxt; /** Creating a EditText for input */
	public static ImageView imgView; /** Creating a ImageView for showing encoded Image */

	/**
	 * On Create sets basic Information
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.encode); /** ContentView is set to Main layout */
		Log.i("Startup", "Setup local Elements");

		/**
		 * Defining Buttons by ID of a special layout
		 */
		encodeContact = (Button) findViewById(R.id.button_contact);
		encodeText = (Button) findViewById(R.id.button_txt_submit);
		inputTxt = (EditText) findViewById(R.id.txtInput);
		imgView = (ImageView) findViewById(R.id.outPutImage);

		Log.i("Startup", "Elemtens created");

		Log.i("Startup", "Initialized onclick");
		
		/*
		 * Defining encodeContact Listener
		 * Redirects to Contactpicker
		 */
		encodeContact.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {

				doLaunchContactPicker(arg0);				
			}
		});

		/**
		 * Creating encode from Text Listener
		 * @see at.fhj.itm.QREncoder.class
		 */
		encodeText.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				QREncoder qre = new QREncoder(); /** Instance a QREncoder Obejct */
				qre.execute(inputTxt.toString()); /** Execute with String input */
			}
		});

	}
	
	/**
	 * Creating Contactexplorer
	 * @param view
	 */
	public void doLaunchContactPicker(View view) {
		Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
				ContactsContract.Contacts.CONTENT_URI);
		startActivityForResult(contactPickerIntent, 1001);
	}

	/**
	 * This function gives us the chance to handle some Data
	 * @see at.fhj.itm.QRDecoder.class
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		String outputText = null;
		if (resultCode == RESULT_OK) {
			Cursor phone = null;
			Cursor email = null;
			Cursor adr = null;
			
			try {
				Uri result = data.getData();
				String id = result.getLastPathSegment();

				phone = getContentResolver().query(Phone.CONTENT_URI, null,
						Phone.CONTACT_ID + "=?", new String[] { id }, null);
				phone.moveToFirst();
				
				email = getContentResolver().query(Email.CONTENT_URI, null,
						Email.CONTACT_ID + "=?", new String[] { id }, null);
				email.moveToFirst();
				
				adr = getContentResolver().query(StructuredPostal.CONTENT_URI, null,
						StructuredPostal.CONTACT_ID + "=?", new String[] { id }, null);
				adr.moveToFirst();
				
				String phoneNumber="";
				String displayName="";
				String mailAddress="";
				String state="";
				String address="";
				
				try{
					phoneNumber = phone.getString(phone.getColumnIndex("data1"));
					displayName = phone.getString(phone.getColumnIndex("display_name"));
					mailAddress = email.getString(email.getColumnIndex("data1"));
					state = adr.getString(adr.getColumnIndex("data8"));
					address = adr.getString(adr.getColumnIndex("data8"))+" "+adr.getString(adr.getColumnIndex("data9"))+" "+adr.getString(email.getColumnIndex("data7"))+" "+adr.getString(email.getColumnIndex("data4"));
				}	
				catch(Exception e){
					Log.e("Error","ok");
				}

				outputText="MECARD:N:"+displayName+";TEL:"+phoneNumber+";URL:;EMAIL:"+mailAddress+";ADR:"+address+";NOTE:;;";
				
				QREncoder qrc = new QREncoder();
				qrc.execute(outputText);
			} 
			catch (Exception e) {
				Log.e("Error", "Failed to get data", e);
			} 
			finally {
				if (phone != null) {
					phone.close();
				}
				if (email != null) {
					email.close();
				}
				if (adr != null) {
					adr.close();
				}
			}
		}
	}
	

}
